package com.example.administrator.personalmail.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.widget.ClearEditText;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegFirstActivity extends AppCompatActivity {
    private Button btnNext;
    private ClearEditText edtPassword;
    private ClearEditText edtPhone;

    private String phoneNumber;
    private String passward;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        init();
    }

    private void init() {
        btnNext = findViewById(R.id.btn_login_next);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkLoginMes()){
                    Intent intent = new Intent(RegFirstActivity.this,RegSecondActivity.class);
                    intent.putExtra("phone",phoneNumber);
                    intent.putExtra("passward",passward);
                    startActivityForResult(intent, Contants.REQUEST_REG);
                }
            }
        });

        edtPassword = findViewById(R.id.edittxt_pwd);
        edtPhone = findViewById(R.id.edittxt_phone);
    }

    // 请求验证码，其中country表示国家代码，如“86”；phone表示手机号码，如“13800138000”
    public void sendCode(String country, String phone) {
        // 注册一个事件回调，用于处理发送验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
//                    Toast.makeText(RegFirstActivity.this,"验证码请求成功",Toast.LENGTH_SHORT).show();
                    // 请注意，此时只是完成了发送验证码的请求，验证码短信还需要几秒钟之后才送达
                } else{
//                    Toast.makeText(RegFirstActivity.this,"验证码请求失败",Toast.LENGTH_SHORT).show();
                }

            }
        });
        // 触发操作
        SMSSDK.getVerificationCode(country, phone);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Contants.REQUEST_SUCCESS){
            RegFirstActivity.this.finish();
        }
    }

    public boolean checkLoginMes(){
        phoneNumber = edtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNumber)){
            Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            return false;
        }
        passward = edtPassword.getText().toString().trim();
        if (TextUtils.isEmpty(passward)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return false;
        }

        sendCode("86",phoneNumber);

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //用完回调要注销掉，否则可能会出现内存泄露
        SMSSDK.unregisterAllEventHandler();
    }
}
